﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using HtmlAgilityPack;
using System.ComponentModel;
using System.Diagnostics;

namespace HTMLParser
{
    class HtmlTree : INotifyPropertyChanged
    {
        private ObservableCollection<HtmlTree> dirs;

        public HtmlTree()
        {
            
        }
        public HtmlTree(HtmlTree parent, HtmlNode di)
            : this()
        {
            this.Parent = parent;
            this.Info = di;
        }

        public HtmlNode Info { get; set; }
        public HtmlTree Parent { get; set; }

        public ObservableCollection<HtmlTree> Htmls
        {
            get
            {
                if (this.dirs == null)
                {
                    try
                    {
                        HtmlNodeCollection dis = Info.ChildNodes;
                        if (dis.Count > 0)
                        {
                            this.dirs = new ObservableCollection<HtmlTree>();
                            foreach (HtmlNode di in dis)
                                this.dirs.Add(new HtmlTree(this, di));
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.StackTrace);
                    }
                }
                return dirs;
            }
        }
        public static ObservableCollection<HtmlTree> InitRoot(string url)
        {
            ObservableCollection<HtmlTree> dts = new ObservableCollection<HtmlTree>();
            HtmlDocument doc = new HtmlDocument();
            doc.Load(url);
            HtmlNodeCollection drvs = doc.DocumentNode.ChildNodes;
            foreach (HtmlNode drv in drvs)
            {
                HtmlTree dt = new HtmlTree(null, drv);
                dts.Add(dt);
            }
            return dts;
        }

        public static ObservableCollection<HtmlTree> InitRoot(HtmlNode node)
        {
            ObservableCollection<HtmlTree> dts = new ObservableCollection<HtmlTree>();
            HtmlTree dt = new HtmlTree(null, node);
            dts.Add(dt);
            return dts;
        }
        public string DisplayText
        {
            get
            {
                string str = null;
                if (this.Info.ChildNodes.Count == 0)
                {
                    str = this.Info.InnerText;
                    str = str.Replace("\r", "");
                    str = str.Replace("\n", "");
                    str = str.Replace(@"&nbsp;", "");
                    str = str.Replace(" ","");
                }
                return str;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        
    }
}
